/*
 * Copyright 2014 Semidata. All rights reserved.
 */
package com.semidata.rtd.core.redis;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author shewei@semidata.com, She Wei
 * 
 */
public class RedisCommand {

	private byte[] data;
	private RedisResponseHandler handler;
	private long expireTime;
	private ReentrantLock lock = new ReentrantLock();
	private boolean flag = false;

	public RedisCommand(byte[] bytes, RedisResponseHandler handler, long timeout) {
		this.data = bytes;
		this.handler = handler;
		this.expireTime = System.currentTimeMillis() + timeout;
	}

	public void handle(Object obj, Exception exception) {
		lock.lock();
		try {
			if (flag)
				return;
			flag = true;
		} finally {
			lock.unlock();
		}
		handler.handle(obj, exception);
	}

	public byte[] getData() {
		return data;
	}

	public long getExpireTime() {
		return expireTime;
	}
}
